//
//  SupervisionDetailController.m
//  homeland
//
//  Created by Alawn on 13-10-20.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "SupervisionDetailController.h"
#import "UIColor+expanded.h"
#import "SupervisionBaseInfoController.h"
#import "MKNetworkKit.h"
#import "UserInfoService.h"
@interface SupervisionDetailController ()

@end

@implementation SupervisionDetailController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.complainView.hidden = YES;//隐藏服务评价
    if (self.config) {
        self.title=[self.config objectForKey:@"key"];
        _dataItem=[self.config objectForKey:@"dataItem"];
        self.txtTitle.text=_dataItem.title;
        self.txtComplainDep.text=_dataItem.serviceOrgName;
        NSString *tempDealStatus=@"未处理";
        if ([_dataItem.dealStatus compare:@"0"]==NSOrderedSame) {
            tempDealStatus=@"未处理";
            self.complainView.hidden=YES;
        }
        if ([_dataItem.dealStatus compare:@"1"]==NSOrderedSame) {
            tempDealStatus=@"处理中";
             self.complainView.hidden=NO;
        }
        if ([_dataItem.dealStatus compare:@"2"]==NSOrderedSame) {
            tempDealStatus=@"已办结";
             self.complainView.hidden=NO;
        }
        self.txtComplainStatus.text=tempDealStatus;
        if (_dataItem.dealContent!=(id)[NSNull null]) {
            NSString *dealContent=[NSString stringWithFormat:@"%@",_dataItem.dealContent];
             self.txtCPContent.text=dealContent;
        }
        self.txtChuLiYiJian.textColor=[UIColor colorWithHexString:@"696969"];
        self.imgGroup1.backgroundColor=[UIColor colorWithHexString:@"dadada"];
//        self.imgGroup2.backgroundColor=[UIColor colorWithHexString:@"e2e2e2"];
//        self.imgGroup3.backgroundColor=[UIColor colorWithHexString:@"cdcdcd"];
    }
    self.navigationItem.rightBarButtonItem = getCustomNavButton(@"基本信息", self, @selector(rightBtAction:));
}
#pragma mark - Button Methods
-(void)rightBtAction:(id)sender
{
    SupervisionBaseInfoController *baseInfoVC = [[SupervisionBaseInfoController alloc]initWithNibName:@"SupervisionBaseInfoController" bundle:nil];
    baseInfoVC._dataitem=_dataItem;
    [self.navigationController pushViewController:baseInfoVC animated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//segment改变事件
//-(IBAction)segmentIndexChanged  {
//    NSString *index=[NSString stringWithFormat:@"%d",self.segComplainSevice.selectedSegmentIndex];
//    [self commitService:index];
//}
#pragma mark - Value Buttons Action
- (IBAction)button1Action:(id)sender
{
    _image1View.image = [UIImage imageNamed:@"RadioButton-selected.png"];
    _image2View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    _image3View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    [self commitService:@"0"];
}
- (IBAction)button2Action:(id)sender
{
    _image1View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    _image2View.image = [UIImage imageNamed:@"RadioButton-selected.png"];
    _image3View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
   [self commitService:@"1"];
}

- (IBAction)button3Action:(id)sender
{
    _image1View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    _image2View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    _image3View.image = [UIImage imageNamed:@"RadioButton-selected.png"];
    [self commitService:@"2"];
}
//评价请求方法
-(void)commitService:(NSString *)segmentindex
{
    MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:JianDuPingJia customHeaderFields:nil];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setValue:_dataItem.Id forKey:@"id"];
    [dic setValue:segmentindex forKey:@"vote_grade"];
    UserInfoService *userInfo=[UserInfoService getUserInfoService];
    if (userInfo.currentLoginUserName && userInfo.currentLoginUserName.length>0) {
        [dic setValue:userInfo.userInfo.Id forKey:@"userId"];
    }
    MKNetworkOperation *op = [engine operationWithPath:@"" params:dic httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        NSLog(@"[operation responseData]-->>%@", [operation responseString]);
        [self loadDataFinished];
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        [self loadDataFaild:[err localizedDescription]];
    }];
    [engine enqueueOperation:op];
    [self startLoadingView];
}
-(void)startLoadingView
{
    loadingView = [[MBProgressHUD alloc]initWithView:self.view];
    loadingView.labelText = @"正在载入...";
    [self.view addSubview:loadingView];
    [loadingView show:YES];
}
-(void)loadDataFinished
{
    [loadingView removeFromSuperview];
    loadingView = nil;
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"消息" message:@"评价已成功,感谢您的评价" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

-(void)loadDataFaild:(NSString *)msg
{
    [loadingView removeFromSuperview];
    loadingView = nil;
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"错误" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
@end
